<template>
    <div style="display: flex;">
        <el-upload  v-loading="loading" :element-loading-text="text" class="img-uploader" :action="action" :show-file-list="false" :on-success="handleImgSuccess"
            :before-upload="beforeImgUpload" :headers="{ Authorization: store.getters.getToken }">
            <img v-if="fileInfo.fileUrl !== ''" :src="fileInfo.fileUrl" class="img" :style="{ width, height }" />
            <el-icon v-else class="img-uploader-icon" :style="{ width, height }">
                <Plus />
            </el-icon>
        </el-upload>
        <el-icon v-if="fileInfo.fileUrl !== ''" class="img-uploader-icon" style="cursor: pointer;"
            :style="{ width, height }" @click="delImg">
            <Delete />
        </el-icon>
    </div>
</template>

<script lang="ts" setup>
import { ref, defineProps, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import type { UploadProps } from 'element-plus'
import serverLocation from '@/utils/server-location'
import store from '@/store'
import request from '@/utils/request'

const loading = ref(false)
const text = ref('上传中...')

// 图片信息
const fileInfo = ref({
    fileName: '',
    fileUrl: '',
})

// 提交数据
const emit = defineEmits(['sendData']);

// 上传地址
const action = serverLocation.localhost + 'upload';

const props = defineProps({
    width: {
        type: String,
        default: '150px',
    },
    height: {
        type: String,
        default: '150px',
    },
})
// 上传成功
const handleImgSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
) => {
    loading.value = false
    fileInfo.value.fileUrl = response.data.fileUrl
    fileInfo.value.fileName = response.data.fileName
    emit('sendData', response.data.fileName)
}

//删除图片
const delImg = async () => {
    text.value = '删除中...'
    loading.value = true
    await request({
        url: 'delete-file/' + fileInfo.value.fileName,
        method: 'delete',
        headers: { Authorization: store.getters.getToken }
    })
    text.value = '上传中...'
    loading.value = false
    fileInfo.value.fileUrl = ''
    fileInfo.value.fileName = ''
    emit('sendData', fileInfo.value.fileName)
}

// 上传前验证
const beforeImgUpload: UploadProps['beforeUpload'] = (rawFile) => {
    if (fileInfo.value.fileName !== '') {
        delImg()
    }

    if (rawFile.type !== 'image/jpeg'&& rawFile.type !== 'image/png'&& rawFile.type !== 'image/jpg') {
        ElMessage.error('图片格式有误')
        return false
    } else if (rawFile.size / 1024 / 1024 > 50) {
        ElMessage.error('图片大小不能超过50M')
        return false
    }
    loading.value = true
    return true
}
</script>

<style>
.img-uploader {
    img {
        width: 200px;
    }
}

.img-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.img-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.img-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 200px;
    height: 128px;
    text-align: center;
}
</style>
